package com.bc.bcbuyerapihttp.controller;

import com.bc.bcapi.service.T_User_google_authService;
import com.bc.bcbuyerapihttp.common.UserContext;
import com.bc.bcmodel.BuyerUserInfo;
import com.bc.bcmodel.domain.T_User_buyer_infoModel;
import com.bc.bcmodel.req.Auth2Req;
import com.bc.commonentity.common.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("gauth")
@Api(value = "卖家钱包接口")
public class Auth2Controller {

    @Autowired
    T_User_google_authService t_user_google_authService;

    @Autowired
    private HttpServletRequest request;

    @ApiOperation("查询是否绑定二次验证")
    @RequestMapping(value = "exist", method = RequestMethod.GET)
    public JsonResult exist() {
        BuyerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        JsonResult js = new JsonResult();
        if (loginInfo == null) {
            js.setCode(0);
            return js;
        }

        //查库
        String user_id = loginInfo.getUser_id();
        js.setData(t_user_google_authService.exists(user_id));

        return js;
    }

    @ApiOperation("绑定二次验证")
    @RequestMapping(value = "bind", method = RequestMethod.POST)
    public JsonResult bind(@RequestBody Auth2Req req) {
        BuyerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        JsonResult js = new JsonResult();
        if (loginInfo == null) {
            js.setCode(0);
            return js;
        }

        return t_user_google_authService.bind(req, loginInfo.getUser_id());
    }

    @ApiOperation("申请二次验证")
    @RequestMapping(value = "apply", method = RequestMethod.POST)
    public JsonResult apply() {
        BuyerUserInfo loginInfo = UserContext.getCurrentContext().getLoginInfo();
        JsonResult js = new JsonResult();
        if (loginInfo == null) {
            js.setCode(0);
            return js;
        }

        //获取域名
        String serverName = request.getServerName();
        return t_user_google_authService.apply(
                loginInfo.getUser_id(), loginInfo.getEmail(), serverName);
    }

}
